package com.shouxin.attendance.utils;

import android.content.Context;
import android.os.Environment;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.UUID;

public class UUIDGenerator {

    private static final String UUID_PATH = FileUtils.TEMP_DIR + "/info";

    private static final String UUID_PATH_FOR_SDCARD = UUID_PATH;

    private static final String UUID_FILENAME = "phone_uuid.tmp";

    public static String getUUID(Context context) {
        String phone_uuid;
        if (!isSdRun()) {
            String filePath = context.getFilesDir().getAbsolutePath() + UUID_PATH;
            phone_uuid = readFile(filePath, UUID_FILENAME);
            if (phone_uuid == null || phone_uuid.length() == 0) {
                UUID uuid = UUID.randomUUID();
                phone_uuid = uuid.toString().replaceAll("-", "").trim();
                writeFile(filePath, phone_uuid, UUID_FILENAME);
            }
            return phone_uuid;
        }

        phone_uuid = readFile(UUID_PATH_FOR_SDCARD, UUID_FILENAME);
        if (phone_uuid == null || phone_uuid.length() == 0) {
            UUID uuid = UUID.randomUUID();
            phone_uuid = uuid.toString().replaceAll("-", "").trim();
            writeFile(UUID_PATH_FOR_SDCARD, phone_uuid, UUID_FILENAME);
        }
        return phone_uuid;
    }

    /**
     * 判断是否有卡在运行
     */
    private static boolean isSdRun() {
        return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
    }

    /**
     * 写入文件
     */
    private static void writeFile(String filePath, String body, String fileName) {
        BufferedWriter bw = null;
        try {
            File file = new File(filePath);
            if (!file.exists()) {
                file.mkdirs();
            }
            bw = new BufferedWriter(new FileWriter(filePath + File.separator + fileName));//字符输出流  写数据到文件
            bw.write(body);
            bw.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (bw != null) {
                    bw.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 读取文件
     */
    private static String readFile(String filePath, String fileName) {
        String result = "";
        BufferedReader br = null;
        try {
            File file = new File(filePath + File.separator + fileName);
            if (file.exists()) {
                //字符输入流  从文件读取数据
                br = new BufferedReader(new FileReader(file));
                result = br.readLine();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (br != null) {
                    br.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return result;
    }
}
